package fr.aura.launcher.client;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

import org.apache.log4j.Logger;

public class LNCSrvMsg {
	/**
	 * 
	 */
	
	private static int byteSize=0;
	static Logger logger = Logger.getLogger(LNCSrvMsg.class.getName());
	
	LNCSrvMsgHeader hdr;
	int	iDataOffset;		// Offset vers debut Data[]
	int	iCmdOffset;
	int	iCmdLength=0;
	int	iOptOffset;
	int	iOptLength=0;
	int	iParm1Offset;
	int	iParm1Length=0;
	int	iParm2Offset;
	int	iParm2Length=0;
	
	String cmd;
	String option;
	String parm1;
	String parm2;
	byte data[];
	
	
	public String getCmd() {
		return cmd;
	}

	public void setCmd(String cmd) {
		this.cmd = cmd;
		
	}

	public String getOption() {
		return option;
	}

	public void setOption(String option) {
		this.option = option;
	}

	public String getParm1() {
		return parm1;
	}

	public void setParm1(String param1) {
		this.parm1 = param1;
	}

	public String getParm2() {
		return parm2;
	}

	public void setParm2(String param2) {
		this.parm2 = param2;
	}

	
	public LNCSrvMsg() {
		hdr = new LNCSrvMsgHeader();
		hdr.setiVersion((short) 1024);
		byteSize=hdr.getByteSize();
		byteSize+=4*9;
		iDataOffset = 4*9;
	}

	public LNCSrvMsgHeader getHdr() {
		return hdr;
	}
	
	public void setHdr(LNCSrvMsgHeader hdr) {
		this.hdr = hdr;
	}

	public byte[] getData() {
		return data;
	}

	public void setData(byte[] data) {
		this.data = data;
	}

	public byte[] toByteArray() {
		byte cmdByte[]=null;
		byte optByte[]=null;
		byte parm1Byte[]=null;
		byte parm2Byte[]=null;
		int totalByte=0;
		byte nullByte[] = new byte[1];
		
		nullByte[0]='\0';
		
		//data conversion
		try {
			if (cmd != null&&cmd.length()!=0) {
				cmdByte=cmd.getBytes("ISO-8859-1");
				totalByte += cmdByte.length;
				iCmdLength = cmdByte.length;
			} else {
				iCmdLength = 0;
			}
			if (option!=null && option.length()!=0) {
				optByte=option.getBytes("ISO-8859-1");
				totalByte += optByte.length+1;
				iOptLength = optByte.length+1;
			} else {
				iOptLength = 1;
				totalByte += 1;
			}
			if (parm1!=null && parm1.length()!=0) {
				parm1Byte=parm1.getBytes("ISO-8859-1");
				totalByte += parm1Byte.length+1;
				iParm1Length = parm1Byte.length+1;
			} else {
				iParm1Length = 1;
				totalByte += 1;
			}
			if (parm2!=null && parm2.length()!=0) {
				if (option!=null && option.compareToIgnoreCase("W")==0) {
					parm2Byte=parm2.getBytes("UTF-8");
					totalByte += parm2Byte.length*2;
					iParm2Length = parm2Byte.length*2;
				} else {
					parm2Byte=parm2.getBytes("ISO-8859-1");
					totalByte += parm2Byte.length;
					iParm2Length = parm2Byte.length;
				}
			} else {
				iParm2Length = 1;
				totalByte += 1;
			}
		} catch (UnsupportedEncodingException e) {
			logger.error("LNCSrvMsg::toByteArray UnsupportedEncodingException when converting message data. There is something wrong with the input data ");
		}
		
		//building up the data byte array
		data = new byte[totalByte];
		System.arraycopy(cmdByte, 0, data, 0, iCmdLength);
		if (optByte!=null) {
			System.arraycopy(optByte, 0, data, iCmdLength, iOptLength-1);
		}
		
		System.arraycopy(nullByte, 0, data, iCmdLength+iOptLength-1, 1);
		
		if (parm1Byte != null) {
			System.arraycopy(parm1Byte, 0, data, iCmdLength+iOptLength, iParm1Length-1);
		}
		System.arraycopy(nullByte, 0, data, iCmdLength+iOptLength+iParm1Length -1, 1);
		
		if (parm2Byte != null) {
			System.arraycopy(parm2Byte, 0, data, iCmdLength+iOptLength+iParm1Length, parm2Byte.length);
		}
		
						
		//calculating offsets		
		iCmdOffset = 0;
		iOptOffset = iCmdOffset + iCmdLength;
		iParm1Offset = iOptOffset + iOptLength;
		iParm2Offset = iParm1Offset + iParm1Length;
		
		
		ByteBuffer myBuffer = ByteBuffer.allocate(byteSize+totalByte);
		myBuffer.order(ByteOrder.LITTLE_ENDIAN);
		this.hdr.setlSize(byteSize+totalByte);
		
		byte headerBytes[] = this.hdr.toByteArray();

		myBuffer.put(headerBytes).putInt(this.iDataOffset).putInt(this.iCmdOffset).putInt(this.iCmdLength).putInt(this.iOptOffset).putInt(this.iOptLength).putInt(this.iParm1Offset).putInt(this.iParm1Length).putInt(this.iParm2Offset).putInt(this.iParm2Length).put(data);
		return myBuffer.array();
	}
	
	public void fromByteArray(byte[] byteArray) {
		ByteBuffer myBuffer = ByteBuffer.wrap(byteArray);
		myBuffer.order(ByteOrder.LITTLE_ENDIAN);
		this.hdr.fromByteArray(myBuffer);
	
	}
	
	public int getByteSize() {
		return byteSize;
	}
}
